package clang.data;

import clang.control.ExecutionThread;

public class VarName implements Expression {

	private Variable ref = null;
	private String name;
	
	public VarName(String name)
	{
		this.name = name;
	}
	
	public String getName()
	{
		return name;
	}
	
	private void find() throws Exception
	{
		if(ref == null)
			ref = ExecutionThread.get().getCurrentBlock().findVar(this);
	}
	
	@Override
	public VarType getType() {
		try {
			find();
		} catch (Exception e) {}
		if(ref == null)
			return VarType.VOID;
		else
			return ref.getType();
	}

	@Override
	public Variable calculate() throws Exception {
		find();
		return ref;
	}

	@Override
	public void setData(Expression e) throws Exception {
		find();
		ref.setData(e);
	}
	
	public void deref()
	{
		ref = null;
	}

	@Override
	public void execute() {
	}
	
	public String toString() {
		try {
			find();
			return ref.toString();
		} catch (Exception e) {
			return "(NULL)";
		}
	}

}
